﻿namespace Dcms.OleDbDAL
{
    using Dcms.Model;
    using System;
    using System.Collections.Generic;

    public class Dcms_FilesComparer : IComparer<Dcms_FileFolderModel>
    {
        private string _sortColumn;
        private string _sortstr = "desc";

        public Dcms_FilesComparer(string sortExpression, string sortstr)
        {
            this._sortColumn = sortExpression;
            this._sortstr = sortstr;
        }

        public int Compare(Dcms_FileFolderModel a, Dcms_FileFolderModel b)
        {
            switch (this._sortColumn.ToLower())
            {
                case "name":
                    return string.Compare(a.FileName, b.FileName, StringComparison.InvariantCultureIgnoreCase);

                case "ext":
                    return string.Compare(a.FileExt, b.FileExt, StringComparison.InvariantCultureIgnoreCase);

                case "size":
                    return string.Compare(a.FileSize, b.FileSize, StringComparison.InvariantCultureIgnoreCase);

                case "modifydate":
                    if (this._sortstr == "asc") return DateTime.Compare(DateTime.Parse(a.FileModifyDate), DateTime.Parse(b.FileModifyDate));
                    if (DateTime.Compare(DateTime.Parse(a.FileModifyDate), DateTime.Parse(b.FileModifyDate)) == 1) return -1;
                    return 1;
            }
            return string.Compare(a.FileName, b.FileName, StringComparison.InvariantCultureIgnoreCase);
        }
    }
}

